// pages/select/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateArr: ['1月班', '2月班', '3月班', '4月班', '5月班', '6月班', '7月班', '8月班', '9月班', '10月班', '11月班', '12月班'],
    height:'',
    isActive:"course",
    student:{
      type:"UI",
      date:"10",
      name:"",
      age:"",
      // 0男1女
      sex:"",
      tel:"",
      cityText:['省','市'],
      // 目前状况
      // 0--应届
      // 1-在校
      // 2-转行
      status:"0",
      school:"",
      major:"",
      education:"",
      // 是否有基础
      exp:"0",
      // 紧急联系人
      tel2: "",
      // 紧急联系人姓名
      name2:"",
      inter:"",
      // 关系
      // 备注
      isAgree:false
    },
    rules:[
      {
        name: 'type',
        rules: { required: true, message: '请选择课程' },
      },
      {
        name: 'date',
        rules: { required: true, message: '请选择学习月份' },
      },
      {
        name: 'sex',
        rules: { required: true, message: '请选择性别' },
      },
      {
        name: 'name',
        rules: { required: true, message: '请填写姓名' },
      },
      {
        name: 'tel',
        rules: [{ required: true, message: '请填写手机号' }, { mobile: true, message: '手机号格式不对' }],
        
      },
      {
        name: 'cityText',
        rules: { required: true, message: '请选择籍贯' },
      },
      {
        name: 'status',
        rules: { required: true, message: '请选择目前状况' },
      },
      {
        name: 'school',
        rules: { required: true, message: '请选择学校' },
      },
      {
        name: 'major',
        rules: { required: true, message: '请选择专业' },
      },
      {
        name: 'education',
        rules: { required: true, message: '请选择学历' },
      },
      {
        name: 'exp',
        rules: { required: true, message: '请选择是否有基础' },
      },
      {
        name: 'name2',
        rules: { required: true, message: '请填写紧急联系人姓名' },
      },
      {
        name: 'tel2',
        rules: [{ required: true, message: '请填写紧急联系人电话' }, { mobile: true, message: '手机号格式不对' }]
      },
      {
        name: 'inter',
        rules: { required: true, message: '请填写与紧急联系人关系' },
      },
    ]

    
  },
  
  selectType:function(e){
    var type = e.currentTarget.dataset.type;
    // console.log(e);
    this.setData({
      "student.type":type
    })
    // console.log(this.data.student.type)
  },
  dateChange:function(e){
    var month = Number(e.detail.value) +1;
    this.setData({
      "student.date":month
    })
  },
  sexChange:function(e){
    // console.log(e.detail);
    this.setData({
      "student.sex":e.detail.value
    })
  },
  userChange:function(e){
    this.setData({
      "student.name":e.detail.value
    })
  },
  telChange: function(e){
    this.setData({
      "student.tel": e.detail.value
    })
  },
  cityChange:function(e){
    this.setData({
      "student.cityText":e.detail.value.slice(0,2)
    })
    // console.log(this.data.student.cityText)
  },
  statusChange:function(e){
    // console.log(e)
    this.setData({
      "student.status":e.detail.value
    })
  },
  schoolChange:function(e){
    this.setData({
      'student.school':e.detail.value
    })
  },
  majorChange: function (e) {
    this.setData({
      'student.major': e.detail.value
    })
  },
  eduChange: function (e) {
    this.setData({
      'student.education': e.detail.value
    })
  },
  expChange: function (e) {
    // console.log(e.detail);
    this.setData({
      "student.exp": e.detail.value
    })
  },
  secondNameChange:function(e){
    this.setData({
      "student.name2": e.detail.value
    })
  },
  secondTelChange: function (e) {
    this.setData({
      "student.tel2": e.detail.value
    })
  },
  secondInterChange: function (e) {
    this.setData({
      "student.inter": e.detail.value
    })
  },
  agreeChange:function(e){
    // console.log(e.detail.value)
    this.setData({
      "student.isAgree":e.detail.value.length?true:false
    })
    // console.log(this.data.student)
  },
  toInfo:function(){
    if (this.data.student.type && this.data.student.date){
      this.setData({
        isActive: 'info'
      })
    }
    
    // this.animate('.course-form', [
    //   { translateX:'-25%' }
    // ], 1000)
  },
  toCourse: function (e) {
    this.setData({
      isActive:'course'
    })
  },
  submit:function(){
    // console.log(this.data.student)
    // 校验数据
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          // this.setData({
          //   error: errors[firstError[0]].message
          // })
          wx.showToast({
            title: errors[firstError[0]].message,
            icon:"none"
          })
        }
      } else {
        wx.showToast({
          title: '校验通过'
        })
        wx.redirectTo({
                url: '../finish/index',
              })
        // 后台保存数据
        // wx.request({
        //   url:'',
        //   data:{},
        //   success:(res)=>{
        //     if(res.status=='200'){
        //       app.globalData.bmInfo.bmRes="pending";
        //       wx.redirectTo({
        //         url: '../finish/index',
        //       })
        //       // coonsole.log(app.globalData.bmInfo.bmRes);
        //     }
        //   }
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let temp = app.globalData.bmInfo;

    if(temp){
      this.setData({
        student:temp
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const res = wx.getSystemInfoSync();
    // console.log(res)
    this.setData({
      height: res.windowHeight
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})